import random
from selenium import webdriver
from selenium.webdriver.support.ui import Select
class common:
    def __init__(self):
        self.web = webdriver.Firefox()
    #主方法
    def main(self,readxlsx,param1,parma2):
        webd = self.tinyshoplogin(readxlsx)
        self.findmudle(webd,param1,parma2)
        self.findtd(webd)
        #self.findimg(webd)
        # self.findiframe(webd,text)
        # self.findselect(webd,readxlsx)
        # self.findinput(webd,readxlsx)
    #登录方法
    def tinyshoplogin(self,readxlsx):
        webd = self.web
        webd.get('http://192.168.0.235/TinyShop_v2.4/index.php?con=admin&act=login')
        webd.implicitly_wait(2)
        webd.maximize_window()
        loginls = ['admin','123456','aaaa']
        putmsg = webd.find_elements_by_tag_name('input')
        for i in range(len(putmsg)):
            if putmsg[i].get_attribute('type') != 'submit':
                putmsg[i].send_keys(loginls[i])
            else:
                putmsg[i].click()
                break
        return webd
    #找模块方法
    def findmudle(self,webd,param1,param2):
        webd.find_element_by_link_text(param1).click()
        webd.find_element_by_link_text(param2).click()
    #点击添加
    def add(self,webd):
        webd.find_element_by_link_text('添加').click()
    #找输入框方法
    def findinput(self,webd,readxlsx):
        msg = webd.find_elements_by_tag_name('input')
        ls = readxlsx
        for i in range(len(msg)):
            try:
                if msg[i].get_attribute('type') == 'text':
                    msg[i].clear()
                    msg[i].send_keys(ls[i])
                elif msg[i].get_attribute('type') == 'checkbox':
                    msg[i].click()
                elif msg[i].get_attribute('type') == 'submit':
                    msg[i].click()
                elif msg[i].get_attribute('type') == 'radio':
                    msg[i].click()
            except Exception as e:
                pass
    #找下拉框方法
    def findselect(self,webd,readxlsx):
       selectls = webd.find_elements_by_tag_name('select')
       ls = readxlsx
       for i in range(len(selectls)):
           Select(selectls[i]).select_by_visible_text(ls[i])
       print(len(selectls))
    #找到登录button并点击
    def findbutton(self,webd):
        button = webd.find_elements_by_class_name('button')
        button[0].click()
    #大输入框传值方法
    def findiframe(self,webd,text):
        webd.find_element_by_xpath('//span/span').click()
        webd.find_element_by_xpath('//textarea').send_keys(text)
    #找到图片方法
    def findimg(self,webd):
        self.findbutton(webd)
        webd.switch_to_frame(webd.find_element_by_name('Openupimg_dialog'))
        webd.find_element_by_xpath('/html/body/div[2]/ul/li[2]').click()
        img = webd.find_elements_by_xpath('//li/img')
        index = random.randint(0,len(img))
        img[index].click()
        webd.find_element_by_class_name('button').click()
        webd.switch_to_default_content()
    #通过td找到新添商品，用于断言
    def findtd(self,webd):
        td = webd.find_elements_by_tag_name('td')
        print(len(td))
        for i in range(len(td)):
            if td[i].text == '服饰':
                print('true')
                break
ls = ['admin','123456','aaaa']
common().main(ls,'商品中心','分类管理','全选')